﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using daSyntaxAnalyser.SyntaxTree;
using CQLQueries.Analyzers;
using System.Reflection;

namespace CQLQueries.SyntaxTree
{
    public class SelectBlockNode
    {
        public object NodeTree;
        public object ValueUp;
        //public object ElementUp;

        public IdNode Id;

        public SelectBlockNode()
        {            
        }

        public void Visit()
        {
            if (SelectIdTest())
            {
                ValueUp = true;
                NodeTree = obj;
                CQL_Interpreter.Instance.GetAllVals(CQL_Interpreter.Instance.ТекущийЗапрос.SelectType, obj);
            }
            else
            {
                ValueUp = false;
                NodeTree = null;
            }
        }

        private static object GetMass<T>(Type type, object obj, string mass, string ind)
        {
            List<T> lob = type.InvokeMember(mass, BindingFlags.GetProperty, null, obj, null) as List<T>;
            return lob[Convert.ToInt32(ind)];
        }

        private Object obj;
        private bool SelectIdTest()
        {
            obj = NodeTree;
            Type type = CQL_Semantic.Instance.запрос.SelectTypePar.SubStrsTypes[0];

            if (NodeTree.GetType().Name != CQL_Semantic.Instance.psevdonims[CQL_Semantic.Instance.запрос.SelectTypePar.SubStrs[0]]) return false;
            if (CQL_Semantic.Instance.запрос.SelectTypePar.SubStrs.Length == 1) return true;
            String str;
            for (int i = 1; i < CQL_Semantic.Instance.запрос.SelectTypePar.SubStrsTypes.Length; ++i)
            {
                type = CQL_Semantic.Instance.запрос.SelectTypePar.SubStrsTypes[i-1];
                str = CQL_Semantic.Instance.запрос.SelectTypePar.SubStrs[i];

                String s = str;
                int f = s.IndexOf('[');
                String index_val;
                bool ismass = false;
                String s2 = s;
                if (f != -1)
                {
                    ismass = true;
                    s2 = s.Substring(0, f);
                }

                try
                {
                    if (ismass)
                    {
                        index_val = s.Substring(f + 1, s.Length - f - 2);
                        if (CQL_Semantic.Instance.запрос.SelectTypePar.SubStrsTypes[i].Name == "FunctionVarDeclarationElement")
                            obj = GetMass<FunctionVarDeclarationElement>(type, obj, s2, index_val);
                        else if (CQL_Semantic.Instance.запрос.SelectTypePar.SubStrsTypes[i].Name == "IdElement")
                            obj = GetMass<IdElement>(type, obj, s2, index_val);
                        else obj = GetMass<object>(type, obj, s2, index_val);
                    }
                    else
                    {
                        obj = type.InvokeMember(str, BindingFlags.GetProperty, null, obj, null);
                    }
                }
                catch
                {
                    return false;
                }                
            }
            return true;
        }
    }
}
